package com.example.mybatis.demo1.config;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;


//这里只是自定义拦截器，要想启动拦截器，还需要配置文件，使拦截器与系统发生关联（或者配置类）
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        System.out.println("session:"+session);
        if (session != null && session.getAttribute("userinfo") != null) {
            System.out.println("未拦截！");
            return true;
        }
        response.setStatus(401);
        System.out.println("拦截");
        return false;

    }
}
